<template>
  <div>
    <div style="background-color: #e6f0ff;width: 100%;height:100%;top:0;left:0;position: absolute">
    </div>
    <div style="position: absolute;top: 310px;left:780px;">
      <h2>登录</h2>
    </div>
    <div style="position: absolute;top: 370px;left:620px;">
      <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">立即登录</el-button>
          <el-button @click="push">注册用户</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
</style>
<script>

import axios from "axios";
import request from "@/Utils/request.js";
import {ElMessage} from "element-plus";
export default {
  data() {
    return {
      ruleForm: {
        account:'',
        password:'',
      },
    };
  },
  methods: {
    push(){
      this.$router.push('/register')
    },
    submitForm() {
        request.post('/user/logintestnp',this.ruleForm).then(res => {
        if(res==="" || this.ruleForm.password === ""||this.ruleForm.account === ""){
          this.$message({
            message:'系统异常',
            type:'error'
          });
        }
        else if(res['isban'] === 0 ){
          this.$message({
            message:'系统异常,该用户已被封禁,请联系管理员',
            type:'error'
          });
        }
        else {
          localStorage.setItem("user",JSON.stringify(res));
          let user = JSON.parse(localStorage.getItem("user"));
          ElMessage.success("登录成功")
          request.get('/api/img/tu/boy').then(response=>{
              request.put('/user/userimage',{
                account:this.ruleForm.account,
                image:response.url,
              }).then(res=>{
              if(user.image==null){
                user.image=response.url
                localStorage.setItem("user", JSON.stringify(user));
              }
            })
          }).catch(error=>{
            console.log(error)
          })
          request.get('/api/zici/sjwm.php?id=88888888&key=88888888').then(response=>{
            request.put('/user/SickName',{
              account:this.ruleForm.account,
              sickname:response.msg,
            }).then(res=>{
              if(user.sickname==null){
                user.sickname=response.msg
              }
              localStorage.setItem("user", JSON.stringify(user));
            })
          }).catch(error=>{
            console.log(error)
          })
          this.$router.push('/home-page');
        }
      })
    },
  }
}
</script>
